import java.util.StringTokenizer;


class P10813_39643 {

	static Scanner10813 scanner = new Scanner10813();
	
	public static void main(String[] args) {
		
		int cases = scanner.getInt();
		
		for (int k = 0; k < cases; k++) {
			process();
		}
		
		
	}

	private static void process() {
		int[][] card = new int[5][5];
		
		for (int j = 0; j < 2; j++)
			for (int k = 0; k < 5; k++) 
				card[j][k] = scanner.getInt();
		
		card[2][0] = scanner.getInt();
		card[2][1] = scanner.getInt();
		card[2][3] = scanner.getInt();
		card[2][4] = scanner.getInt();
		
		
		for (int j = 3; j < 5; j++)
			for (int k = 0; k < 5; k++) {
				card[j][k] = scanner.getInt();
		}
		
		
		
		boolean[][] cardMarked = new boolean[5][5]; 
		cardMarked[2][2] = true;
		
//		print(card);
//		print(cardMarked);
		
		int rounds = 0;
		boolean winner = false;
		
		for (int k = 0; k < 75; k++) {
			int numberAnounced = scanner.getInt();
			if (!winner)
				mark(numberAnounced, card, cardMarked);
//			if (!winner) {
//				System.out.println(numberAnounced);
//				print(card);
//				print(cardMarked);
//				System.out.println();
//			}
//			
			if (!winner && isWinner(cardMarked)) {
				winner = true;
				rounds = k;
			}
		}
		
		System.out.println("BINGO after " + (rounds + 1) + " numbers announced");
		
	}

	private static void print(boolean[][] cardMarked) {
		for (int k = 0; k < 5; k++) {
			for (int j = 0; j < 5; j++) {
				System.out.print(cardMarked[k][j] + "\t");
			}
			System.out.println();
		}
		
	}
	
	private static void print(int[][] cardMarked) {
		for (int k = 0; k < 5; k++) {
			for (int j = 0; j < 5; j++) {
				System.out.print(cardMarked[k][j] + "\t");
			}
			System.out.println();
		}
		
	}

	private static boolean isWinner(boolean[][] cardMarked) {
		for (int k = 0; k < 5; k++) {
			int counter = 0;
			for (int j = 0; j < 5; j++) {
				if (cardMarked[k][j]) counter++;
			}
			if (counter == 5) return true;
		}
		
		for (int k = 0; k < 5; k++) {
			int counter = 0;
			for (int j = 0; j < 5; j++) {
				if (cardMarked[j][k]) counter++;
			}
			if (counter == 5) return true;
		}
		
		int counter = 0;
		
		for (int k = 0; k < 5; k++) {
			if (cardMarked[k][k]) counter++;
		}
		
		if (counter == 5) return true;
		
		counter = 0;
		
		for (int k = 0; k < 5; k++) {
			if (cardMarked[k][5 - (k + 1)]) counter++;
		}
		
		if (counter == 5) return true;
		
		return false;
	}

	private static void mark(int numberAnounced, int[][] card, boolean[][] cardMarked) {
		
		for (int k = 0; k < 5; k++) {
			for (int j = 0; j < 5; j++) {
				if (card[k][j] == numberAnounced) {
					cardMarked[k][j] = true;
					return;
				}
			}
		}
		
	}
	
}

class Scanner10813 {

    StringTokenizer st = null;

    int getInt() {
            if (st == null)
                    st = new StringTokenizer(Reader10813.readLn());
            while (!st.hasMoreTokens())
                    st = new StringTokenizer(Reader10813.readLn());
            return Integer.parseInt(st.nextToken());
    }

}

class Reader10813 {
    static String readLn() {
            String newLine = System.getProperty("line.separator");
            StringBuffer buffer = new StringBuffer();
            int car = -1;
            try {
                    car = System.in.read();
                    while ((car > 0) && (car != newLine.charAt(0))) {
                            buffer.append((char) car);
                            car = System.in.read();
                    }
                    if (car == newLine.charAt(0))
                            System.in.skip(newLine.length() - 1);
            } catch (java.io.IOException e) {
                    return (null);
            }
            if ((car < 0) && (buffer.length() == 0))
                    return (null);
            return (buffer.toString()).trim();
    }
}
